﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace HCMServices.DAO
{
    /// <summary>
    /// This class process for PHUONG Table
    /// </summary>
    public class WardDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of class
        /// </summary>
        /// <param name="connectString">String indentify how to connect database</param>
        public WardDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        /// <summary>
        /// This function gets all PHUONG rows in database
        /// </summary>
        /// <returns>List of PHUONG rows</returns>
        public List<WardDTO> GetAllPhuongDTO()
        {
            List<WardDTO> lstPhuongDTO = new List<WardDTO>();

            IList<PHUONG> query = (from PHUONG ph in db.Ward
                                       select ph).ToList();

            foreach (PHUONG ph in query)
            {
                WardDTO temp = new WardDTO();
                temp.Id = ph.MaPhuong;
                temp.Name = ph.TenPhuong;

                lstPhuongDTO.Add(temp);
            }

            return lstPhuongDTO;
        }

        /// <summary>
        /// This function search PHUONG row base on ID Column 
        /// </summary>
        /// <param name="id">ID to find</param>
        /// <returns>PHUONG row if id was found or null</returns>
        public WardDTO Search(int id)
        {
            WardDTO phuongDTO = new WardDTO();

            IList<PHUONG> query = (from PHUONG phuong in db.Ward
                                   where phuong.MaPhuong == id
                                   select phuong).ToList();

            foreach (PHUONG p in query)
            {
                phuongDTO.Id = p.MaPhuong;
                phuongDTO.Name = p.TenPhuong;
                return phuongDTO;               
            }

            return null;                        
        }

        /// <summary>
        /// This function retrieve id of PHUONG base on TenPhuong
        /// </summary>
        /// <param name="name">String of TenPhuong want to find</param>
        /// <returns>MaPhuong of PHUONG was found or -1</returns>
        public int GetIdByName(string name)
        {
            int maPhuong = -1;

            IList<PHUONG> query = (from PHUONG phuong in db.Ward
                                   where phuong.TenPhuong == name
                                   select phuong).ToList();

            foreach (PHUONG p in query)
            {
                maPhuong = p.MaPhuong;
                return maPhuong;                // Return first element
            }

            return -1;                        // NOT FOUND
        }
    }
}